/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_FS_DETAILS_FILE_WIN_H_
#define NODE_FS_DETAILS_FILE_WIN_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include "node/macros.h"

namespace node {
    namespace fs {
        
        class File
        {
        public:
            enum OpenMode
            {
                OM_CREAT = 0x01,        // if file not exists, create new
                OM_EXCL = 0x02,         // if file not exists, fail
                OM_TRUNC = 0x04,        // open file and set size to 0
                OM_READ = 0x08,         // open file for read
                OM_WRITE = 0x10,        // open file for write
                OM_CREAT_ALWAYS = 0x20, // always create new file
                OM_APPEND = OM_WRITE | OM_CREAT,
                OM_LOGFILE = OM_CREAT_ALWAYS | OM_WRITE,
            };

            enum OpenFlags
            {
                OF_NO_BUFFERING = 0x01,
            };

            File(void);
            File(const char *filename, int openmode = OM_CREAT | OM_READ | OM_WRITE, int flags = 0);
            ~File(void);

            bool Open(const char *filename, int openmode = OM_CREAT | OM_READ | OM_WRITE, int flags = 0);
            void Close(void);

            int Read(char *buffer, int length);
            int Write(const char *buffer, int length);
            int Seek(int offset, int method);
            bool Flush(void);

            inline int get_errno(void) const {
                return errno_;
            }
            inline const ::std::string& get_errmsg(void) const {
                return errmsg_;
            }
            inline bool IsValid(void) const {
                return handle_ != INVALID_HANDLE_VALUE;
            }
            inline const ::std::string &get_filename(void) const {
                return filename_;
            }

            long int GetFileSize(void);
            bool GetTimes(struct tm *ctime, struct tm *mtime, struct tm *atime);

            static bool Exists(const char *filename);
        private:
            void GenErrorInfo(void);

            HANDLE handle_;
            ::std::string filename_;

            int errno_;
            ::std::string errmsg_;
            DISALLOW_COPY_AND_ASSIGN(File);
        };

    }
}

#endif // NODE_FS_DETAILS_FILE_WIN_H_
